class Solution {
public:
    bool backspaceCompare(string s, string t) {
        string stack1 = "", stack2 = "";
        for(auto& e : s)
        {
            if(e != '#')
                stack1 += e;
            else if(stack1.size())
                stack1.pop_back();
        }
        for(auto& e : t)
        {
            if(e != '#')
                stack2 += e;
            else if(stack2.size())
                stack2.pop_back();
        }
        return stack1 == stack2;
    }
};